package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Subjects;
import com.xmy.cultivate.entity.TeacherPoint;
import com.xmy.cultivate.entity.TrainAuditTeacherDetail;
import com.xmy.cultivate.entity.TrainAuditTeacherSet;
import com.xmy.cultivate.service.ITrainAuditTeacherDetailService;
import com.xmy.cultivate.service.ITrainAuditTeacherSetService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.IOException;

/**
 * <p>
 * 审核老师设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-07-01
 */
@RestController
@RequestMapping("/api/trainAuditTeacherSet")
public class TrainAuditTeacherSetController {

    @Autowired
    ITrainAuditTeacherSetService iTrainAuditTeacherSetService;

    @Autowired
    ITrainAuditTeacherDetailService iTrainAuditTeacherDetailService;


    @GetMapping("/findAll")
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                          @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
                          @RequestParam(value = "quarterNum", required = false) String quarterNum, //学期
                          @RequestParam(value = "departmentId", required = false) Long departmentId,  //年份
                          @RequestParam(value = "type", required = false) Integer type,
                          @RequestParam(value = "subType", required = false) Integer subType,// 教学审核时（type为1时）1：练课、2：备课、4：习题、5：发音
                          @RequestParam(value = "name", required = false) String name
    ) {
        IPage<TrainAuditTeacherSet> iPage = new Page<>(pageNum, pageSize);
        iTrainAuditTeacherSetService.findAll(iPage, yearPart, quarterNum, departmentId, type, name, subType);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }



    @PostMapping("/save")
    public Result save(@RequestBody TrainAuditTeacherSet  trainAuditTeacherSet){

        QueryWrapper<TrainAuditTeacherSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",trainAuditTeacherSet.getYearPart());
        queryWrapper.eq("quarter_num",trainAuditTeacherSet.getQuarterNum());
        queryWrapper.eq("teacher_id",trainAuditTeacherSet.getTeacherId());
        queryWrapper.eq("department_id",trainAuditTeacherSet.getDepartmentId());
        queryWrapper.eq("type",trainAuditTeacherSet.getType());
        queryWrapper.eq("sub_type",trainAuditTeacherSet.getSubType());
        long count = iTrainAuditTeacherSetService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该年份该学期已存在该老师，无法添加");
        }
        boolean save = iTrainAuditTeacherSetService.save(trainAuditTeacherSet);
        if (save){
            return new Result(Code.OK, null, Code.SAVE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.SAVE_ERROR_MSG);

    }
    @PutMapping("/update")
    public Result update(@RequestBody TrainAuditTeacherSet  trainAuditTeacherSet){
        TrainAuditTeacherSet trainAuditTeacherSetOld = iTrainAuditTeacherSetService.getById(trainAuditTeacherSet.getId());
        if (trainAuditTeacherSetOld == null) {
            return new Result(Code.UPDATE_ERROR, null, "该条数据不存在");
        }

        QueryWrapper<TrainAuditTeacherSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",trainAuditTeacherSet.getId());
        queryWrapper.eq("year_part",trainAuditTeacherSetOld.getYearPart());
        queryWrapper.eq("quarter_num",trainAuditTeacherSetOld.getQuarterNum());
        queryWrapper.eq("teacher_id",trainAuditTeacherSet.getTeacherId());
        queryWrapper.eq("department_id",trainAuditTeacherSetOld.getDepartmentId());
        queryWrapper.eq("type",trainAuditTeacherSetOld.getType());
        queryWrapper.eq("sub_type",trainAuditTeacherSet.getSubType());
        long count = iTrainAuditTeacherSetService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该年份该学期已存在该老师，无法添加");
        }
        boolean update = iTrainAuditTeacherSetService.updateById(trainAuditTeacherSet);
        if (update){
            return new Result(Code.OK, null, Code.UPDATE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.UPDATE_ERROR_MSG);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestBody TrainAuditTeacherSet  trainAuditTeacherSet){

        QueryWrapper<TrainAuditTeacherDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit_teacher_set_id",trainAuditTeacherSet.getId());
        long count = iTrainAuditTeacherDetailService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.DELETE_ERROR, null, "该条数据有教师未删除，不可删除");
        }

        boolean delete = iTrainAuditTeacherSetService.removeById(trainAuditTeacherSet.getId());
        if (delete){
            return new Result(Code.OK, null, Code.DELETE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.DELETE_ERROR_MSG);
    }


}
